package com.googlecode.bipol.war.services;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.googlecode.bip.dao.DAOFactory;
import com.googlecode.bip.entity.IQuestion;
import com.googlecode.bip.entity.ITopic;
import com.googlecode.bip.entity.Topic;

public class TopicManagerImpl implements TopicManager, Serializable {
    // Seem to must implement java.io.Serializable 
    private static final long serialVersionUID = 1L;

    @Override
    public ITopic createTopic(String name, String description, String idLocal) throws ServiceException {
    	// TODO Check if the topics already exists
    	// DAOFactory.getDAOFactory().getTopicDAO().findTopic(name, description, idlocal)
        // Create the topic based on properties
        Topic t = new Topic();
        t.setName(name);
        t.setDescription(description);
        t.setIdLocal(idLocal);
        // Insert the new topic
        try {
            if (DAOFactory.getDAOFactory().getTopicDAO().insertTopic(t)) {
                return t;
            } else {
                throw new ServiceException("Error when saving topic: Unknown reason (please contact the administrator)");
            }
        } catch(Exception e) {
            throw new ServiceException("Error when saving topic: "+e.getMessage());
        }
    }
    
    @Override
    public ITopic removeTopic(int idtopic, String idlocal) throws ServiceException {
    	// Check that topic exists
    	ITopic t = ServicesHelper.getExistingTopic(idtopic, idlocal);
    	// Delete topic
    	try {
    		if (DAOFactory.getDAOFactory().getTopicDAO().deleteTopicLocal(t.getIdTopic(),t.getIdLocal())) {
                return t;
            } else {
                throw new ServiceException("Error when deleting topic: Unknown reason (please contact the administrator)");
            }
        } catch(Exception e) {
            throw new ServiceException("Error when deleting topic: "+e.getMessage());
        }
    }

    public List<ITopic> getAllTopics() throws ServiceException {
        try {
            List<ITopic> topics = DAOFactory.getDAOFactory().getTopicDAO().getAllTopics();
            return topics;
        } catch(Exception e) {
            throw new ServiceException("Error when getting topics: "+e.getMessage());
        }
    }
    
    // TEST
    public List getSurvey() throws ServiceException {
    	List survey = new ArrayList();
    	try {
            List<ITopic> topics = DAOFactory.getDAOFactory().getTopicDAO().getAllTopics();
            for(ITopic t : topics) {
            	if ("en".equals(t.getIdLocal())) {
            		if (t.getIdTopic() >= 0) {
	            		survey.add(t);
	            		List<IQuestion> questions= DAOFactory.getDAOFactory().getQuestionDAO().findQuestions(t.getIdTopic(),"en");
	            		for (IQuestion q : questions) {
	            			survey.add(q);
	            		}
            		}
            	}
            }
        } catch(Exception e) {
            throw new ServiceException("Error when getting survey: "+e.getMessage());
        }
    	return survey;
    }
    
}
